<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<script type="text/javascript">

    var socket;
    if(window.WebSocket) {

        socket = new WebSocket("ws://localhost:8899/ws");

        socket.onmessage = function (ev) {
            var ta = document.getElementById("responseTxt");
            ta.value = ta.value + "\n" + ev.data;
        }
        
        socket.onopen = function (ev) {
            var ta = document.getElementById("responseTxt");
            ta.value = "连接开启！";
        }

        socket.onclose = function (ev) {
            var ta = document.getElementById("responseTxt");
            ta.value = ta.value + "\n" + "连接关闭";
        }


    }else {
        alert("浏览器不支持WebSocket");
    }

    function sendMsg(message) {

        if(window.WebSocket) {

            if(socket.readyState == WebSocket.OPEN) {

                socket.send(message);

            } else {
                alert("连接尚未开启！");
            }

        }

    }

</script>

<form onsubmit="return false;">

    <textarea id="message" style="width: 400px; height: 200px;"></textarea>
    <input type="button" value="发送数据" onclick="javascript: sendMsg(this.form.message.value)" />
    <h3>服务器输出</h3>
    <textarea id="responseTxt" style="width: 400px; height: 300px;"></textarea>
    <input type="button" value="清空内容" onclick="javascript: document.getElementById('responseTxt').value = ''" />
</form>

</body>
</html>